
local pool = {}
setmetatable(pool,pool)
ac.pool = pool

local mt = {}
pool.__index = mt

--结构
mt.type = 'pool'

-- 创建一个池
function pool:__call()
    local tbl = setmetatable({
        items = {},
        index = 0,
    },self)
    return tbl
end

-- 添加
function mt:add(object, chance)
    table.insert(self.items, { chance, object })
    self.index = self.index + chance
    return self
end 

-- 取出随机一个成员
function mt:random()
    if self.index > 0 then 
        local index = math.random(1, self.index)
        local total = 0
        local value 
        for i, o in ipairs(self.items) do 
            total = total + o[1]
            if index <= total then 
                value = o[2]
                break
            end
        end 
        return value
    end
end

return pool 